package com.mjh.zhujianju.controller.party;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.base.service.FileService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedResult;
import com.livew.framework.pojo.AjaxResult;
import com.mjh.zhujianju.model.party.BuildingClassDetailEO;
import com.mjh.zhujianju.model.party.BuildingClassEO;
import com.mjh.zhujianju.model.party.BuildingMeetingEO;
import com.mjh.zhujianju.service.party.BuildingClassDetailService;
import com.mjh.zhujianju.service.party.BuildingClassService;
import com.mjh.zhujianju.vo.party.BuildingClassDetailVO;
import com.mjh.zhujianju.vo.party.BuildingClassVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: Alex
 * @version: V1.0
 * @project: zhujianju
 * @package: com.mjh.zhujianju.controller.party
 * @description: 微课堂
 * @date: 2018/8/16
 * @time: 下午4:33
 */
@Controller()
@RequestMapping("party/class")
public class ClassController extends BaseController<BuildingClassEO> {

    @Autowired
    private BuildingClassService buildingClassService;

    @Autowired
    private BuildingClassDetailService buildingClassDetailService;

    @Autowired
    private FileService fileService;

    @Override
    protected BaseService<BuildingClassEO> getService() {
        return buildingClassService;
    }

    @RequestMapping(value="saveClassInfo",method=RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveClassInfo(BuildingClassVO buildingNewsVO) {
        if(buildingNewsVO==null) {
            return AjaxResult.getResult(Boolean.FALSE, "参数错误" );
        }
        try {
            buildingClassService.saveInfo(buildingNewsVO);
        } catch (XTBusinessException e) {
            e.printStackTrace();
            return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
        }
        return AjaxResult.getResult(Boolean.TRUE, "保存成功");
    }

    @RequestMapping(value="saveClassDetailInfo",method=RequestMethod.POST)
    @ResponseBody
    public AjaxResult saveClassDetailInfo(BuildingClassDetailVO buildingClassDetailVO) {
        if(buildingClassDetailVO==null) {
            return AjaxResult.getResult(Boolean.FALSE, "参数错误" );
        }
        try {
            buildingClassDetailService.saveInfo(buildingClassDetailVO);
        } catch (XTBusinessException e) {
            e.printStackTrace();
            return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
        }
        return AjaxResult.getResult(Boolean.TRUE, "保存成功");
    }

    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getPage(BuildingClassVO buildingNewsVO) {
        PagedResult<BuildingClassVO> list = buildingClassService.findBuildingClassPagination(buildingNewsVO);
        return AjaxResult.getResult(list);
    }

    @RequestMapping(value = "getDetailPage", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getDetailPage(BuildingClassDetailVO buildingClassDetailVO) {
        PagedResult<BuildingClassDetailVO> list = buildingClassDetailService.findBuildingClassDetailPagination(buildingClassDetailVO);
        return AjaxResult.getResult(list);
    }

    @RequestMapping(value = "editNews/{id}", method = RequestMethod.GET)
    public String editNews(@PathVariable("id") Long id, Model model) {
        BuildingClassVO buildingClassVO = buildingClassService.queryBuildingClassInfo(id);
        model.addAttribute("buildingClass", buildingClassVO);
        return "party/class/create";
    }

    @RequestMapping(value = "detailNews/{id}", method = RequestMethod.GET)
    public String detailNews(@PathVariable("id") Long id, Model model) {
        BuildingClassVO buildingClassVO = buildingClassService.queryBuildingClassInfo(id);
        model.addAttribute("buildingClass", buildingClassVO);
        return "party/class/detail";
    }

    @RequestMapping(value = "delDetail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult delDetail(@PathVariable("id") Long id, Model model) {

        if (id == null) {
            return AjaxResult.getResult(Boolean.FALSE, "参数错误");
        }
        try {
            BuildingClassDetailEO buildingClassDetailEO = new BuildingClassDetailEO();
            buildingClassDetailEO.setId(id);
            buildingClassDetailService.delByPrimaryKey(buildingClassDetailEO);
        } catch (XTBusinessException e) {
            e.printStackTrace();
            return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
        }
        return AjaxResult.getResult(Boolean.TRUE, "删除成功");

    }

    @Override
    public AjaxResult findById(@PathVariable("id") Long id) {
        if (id == null) {
            return AjaxResult.getResult(Boolean.FALSE, "参数错误");
        }
        BuildingClassVO t = null;
        try {
            t =this.buildingClassService.getInfo(id);
        } catch (XTBusinessException e) {
            e.printStackTrace();
            return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
        }
        return AjaxResult.getResult(t);
    }
}
